home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vbpcopy / jmtree.bas < prev    next >
Encoding:
BASIC Source File  |  1998-10-01  |  17.4 KB  |  397 lines

  1. Attribute VB_Name = "DirectoryTreeSubs"
  2. Option Explicit
  3.  
  4.     
  5. ' Brought to you by:
  6. '  Brad Martinez
  7. '  btmtz@aol.com
  8. '  http://members.aol.com/btmtz/vb
  9.  
  10. '///////////////////////////////////////////////////////////////////////////////////////////////////////////
  11.  
  12. ' A little info...
  13. ' Objects in the shellÆs namespace are assigned item identifiers and item
  14. ' identifier lists. An item identifier uniquely identifies an item within its parent
  15. ' folder. An item identifier list uniquely identifies an item within the shellÆs
  16. ' namespace by tracing a path to the item from the desktop.
  17.  
  18. '///////////////////////////////////////////////////////////////////////////////////////////////////////////
  19.  
  20. ' An item identifier is defined by the variable-length SHITEMID structure.
  21. ' The first two bytes of this structure specify its size, and the format of
  22. ' the remaining bytes depends on the parent folder, or more precisely
  23. ' on the software that implements the parent folderÆs IShellFolder interface.
  24. ' Except for the first two bytes, item identifiers are not strictly defined, and
  25. ' applications should make no assumptions about their format.
  26.     Type SHITEMID   ' mkid
  27.         cb As Long       ' Size of the ID (including cb itself)
  28.         abID() As Byte  ' The item ID (variable length)
  29.     End Type
  30.  
  31. ' The ITEMIDLIST structure defines an element in an item identifier list
  32. ' (the only member of this structure is an SHITEMID structure). An item
  33. ' identifier list consists of one or more consecutive ITEMIDLIST structures
  34. ' packed on byte boundaries, followed by a 16-bit zero value. An application
  35. ' can walk a list of item identifiers by examining the size specified in each
  36. ' SHITEMID structure and stopping when it finds a size of zero. A pointer
  37. ' to an item identifier list, is sometimes called a PIDL (pronounced piddle)
  38.     Type ITEMIDLIST   ' idl
  39.         mkid As SHITEMID
  40.     End Type
  41.  
  42. ' Converts an item identifier list to a file system path.
  43. ' Returns TRUE if successful or FALSE if an error occurs, for example,
  44. ' if the location specified by the pidl parameter is not part of the file system.
  45.     Declare Function SHGetPathFromIDList Lib "shell32.dll" Alias "SHGetPathFromIDListA" (ByVal pIdl As Long, ByVal pszPath As String) As Long
  46.  
  47. ' Retrieves the location of a special (system) folder.
  48. ' Returns NOERROR if successful or an OLE-defined error result otherwise.
  49.     Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" (ByVal hwndOwner As Long, ByVal nFolder As Long, pIdl As ITEMIDLIST) As Long
  50.  
  51. ' SHGetSpecialFolderLocation successful rtn val
  52.     Public Const NOERROR = 0
  53.  
  54. ' SHGetSpecialFolderLocation nFolder params:
  55. ' Most folder locations are stored in:
  56. ' [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders]
  57. ' Value specifying the types of folders to be listed in the dialog box as well as other options.
  58. ' This member can be 0 or one of the following values:
  59.  
  60. ' Windows desktop, virtual folder at the root of the name space.
  61.     Public Const CSIDL_DESKTOP = &H0
  62.  
  63. ' File system directory that contains the user's program groups
  64. ' (which are also file system directories).
  65.     Public Const CSIDL_PROGRAMS = &H2
  66.  
  67. ' Control Panel, virtual folder containing icons for the control panel applications.
  68.     Public Const CSIDL_CONTROLS = &H3
  69.  
  70. ' Printers folder, virtual folder containing installed printers.
  71.     Public Const CSIDL_PRINTERS = &H4
  72.  
  73. ' File system directory that serves as a common respository for documents.
  74.     Public Const CSIDL_PERSONAL = &H5   ' (Documents folder)
  75.  
  76. ' File system directory that contains the user's favorite Internet Explorer URLs.
  77.     Public Const CSIDL_FAVORITES = &H6
  78.  
  79. ' File system directory that corresponds to the user's Startup program group.
  80.     Public Const CSIDL_STARTUP = &H7
  81.  
  82. ' File system directory that contains the user's most recently used documents.
  83.     Public Const CSIDL_RECENT = &H8   ' (Recent folder)
  84.  
  85. ' File system directory that contains Send To menu items.
  86.     Public Const CSIDL_SENDTO = &H9
  87.  
  88. ' Recycle bin, file system directory containing file objects in the user's recycle bin.
  89. ' The location of this directory is not in the registry; it is marked with the hidden and
  90. ' system attributes to prevent the user from moving or deleting it.
  91.     Public Const CSIDL_BITBUCKET = &HA
  92.  
  93. ' File system directory containing Start menu items.
  94.     Public Const CSIDL_STARTMENU = &HB
  95.  
  96. ' File system directory used to physically store file objects on the desktop
  97. ' (not to be confused with the desktop folder itself).
  98.     Public Const CSIDL_DESKTOPDIRECTORY = &H10
  99.  
  100. ' My Computer, virtual folder containing everything on the local computer: storage
  101. ' devices, printers, and Control Panel. The folder may also contain mapped network drives.
  102.     Public Const CSIDL_DRIVES = &H11
  103.  
  104. ' Network Neighborhood, virtual folder representing the top level of the network hierarchy.
  105.     Public Const CSIDL_NETWORK = &H12
  106.  
  107. ' File system directory containing objects that appear in the network neighborhood.
  108.     Public Const CSIDL_NETHOOD = &H13
  109.  
  110. ' Virtual folder containing fonts.
  111.     Public Const CSIDL_FONTS = &H14
  112.  
  113. ' File system directory that serves as a common repository for document templates.
  114.     Public Const CSIDL_TEMPLATES = &H15   ' (ShellNew folder)
  115.  
  116. '========================================================
  117.  
  118. ' Frees memory allocated by SHBrowseForFolder()
  119.     Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
  120.  
  121. ' Displays a dialog box that enables the user to select a shell folder.
  122. ' Returns a pointer to an item identifier list that specifies the location
  123. ' of the selected folder relative to the root of the name space. If the user
  124. ' chooses the Cancel button in the dialog box, the return value is NULL.
  125.     Declare Function SHBrowseForFolder Lib "shell32.dll" Alias "SHBrowseForFolderA" (lpBrowseInfo As BROWSEINFO) As Long ' ITEMIDLIST
  126.  
  127. ' Contains parameters for the the SHBrowseForFolder function and receives
  128. ' information about the folder selected by the user.
  129.     Public Type BROWSEINFO   ' bi
  130.         
  131.         ' Handle of the owner window for the dialog box.
  132.         hOwner As Long
  133.         
  134.         ' Pointer to an item identifier list (an ITEMIDLIST structure) specifying the location
  135.         ' of the "root" folder to browse from. Only the specified folder and its subfolders
  136.         ' appear in the dialog box. This member can be NULL, and in that case, the
  137.         ' name space root (the desktop folder) is used.
  138.         pidlRoot As Long
  139.         
  140.         ' Pointer to a buffer that receives the display name of the folder selected by the
  141.         ' user. The size of this buffer is assumed to be MAX_PATH bytes.
  142.         pszDisplayName As String
  143.         
  144.         ' Pointer to a null-terminated string that is displayed above the tree view control
  145.         ' in the dialog box. This string can be used to specify instructions to the user.
  146.         lpszTitle As String
  147.         
  148.         ' Value specifying the types of folders to be listed in the dialog box as well as
  149.         ' other options. This member can include zero or more of the following values below.
  150.         ulFlags As Long
  151.         
  152.         ' Address an application-defined function that the dialog box calls when events
  153.         ' occur. For more information, see the description of the BrowseCallbackProc
  154.         ' function. This member can be NULL.
  155.         lpfn As Long
  156.         
  157.         ' Application-defined value that the dialog box passes to the callback function
  158.         ' (if one is specified).
  159.         lParam As Long
  160.         
  161.         ' Variable that receives the image associated with the selected folder. The image
  162.         ' is specified as an index to the system image list.
  163.         iImage As Long
  164.     
  165.     End Type
  166.  
  167. ' BROWSEINFO ulFlags values:
  168. ' Value specifying the types of folders to be listed in the dialog box as well as
  169. ' other options. This member can include zero or more of the following values:
  170.  
  171. ' Only returns file system directories. If the user selects folders
  172. ' that are not part of the file system, the OK button is grayed.
  173.     Public Const BIF_RETURNONLYFSDIRS = &H1
  174.  
  175. ' Does not include network folders below the domain level in the tree view control.
  176. ' For starting the Find Computer
  177.     Public Const BIF_DONTGOBELOWDOMAIN = &H2
  178.  
  179. ' Includes a status area in the dialog box. The callback function can set
  180. ' the status text by sending messages to the dialog box.
  181.     Public Const BIF_STATUSTEXT = &H4
  182.  
  183. ' Only returns file system ancestors. If the user selects anything other
  184. ' than a file system ancestor, the OK button is grayed.
  185.     Public Const BIF_RETURNFSANCESTORS = &H8
  186.  
  187. ' Only returns computers. If the user selects anything other
  188. ' than a computer, the OK button is grayed.
  189.     Public Const BIF_BROWSEFORCOMPUTER = &H1000
  190.  
  191. ' Only returns (network) printers. If the user selects anything other
  192. ' than a printer, the OK button is grayed.
  193.     Public Const BIF_BROWSEFORPRINTER = &H2000
  194.  
  195.     Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Boolean
  196.  
  197. Declare Function DrawIconEx Lib "user32" (ByVal hdc As Long, ByVal xLeft As Long, ByVal yTop As Long, ByVal hIcon As Long, ByVal cxWidth As Long, ByVal cyWidth As Long, ByVal istepIfAniCur As Long, ByVal hbrFlickerFreeDraw As Long, ByVal diFlags As Long) As Boolean
  198. ' DrawIconEx() diFlags values:
  199. Public Const DI_MASK = &H1
  200. Public Const DI_IMAGE = &H2
  201. Public Const DI_NORMAL = &H3
  202. Public Const DI_COMPAT = &H4
  203. Public Const DI_DEFAULTSIZE = &H8
  204.  
  205. Declare Function SHGetFileInfo Lib "Shell32" Alias "SHGetFileInfoA" (ByVal pszPath As Any, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long
  206.  
  207. ' pszPath:
  208. ' Pointer to a buffer that contains the path and filename. Both absolute and
  209. ' relative paths are valid. If uFlags includes the SHGFI_PIDL, value pszPath
  210. ' must be the address of an ITEMIDLIST structure that contains the list of
  211. ' item identifiers that uniquely identifies the file within the shell's name space.
  212. ' This string can use either short (the 8.3 form) or long filenames.
  213.  
  214. ' dwFileAttributes:
  215. ' Array of file attribute flags (FILE_ATTRIBUTE_ values). If uFlags does not
  216. ' include the SHGFI_USEFILEATTRIBUTES value, this parameter is ignored.
  217.  
  218. Public Const FILE_ATTRIBUTE_READONLY = &H1
  219. Public Const FILE_ATTRIBUTE_HIDDEN = &H2
  220. Public Const FILE_ATTRIBUTE_SYSTEM = &H4
  221. Public Const FILE_ATTRIBUTE_DIRECTORY = &H10
  222. Public Const FILE_ATTRIBUTE_ARCHIVE = &H20
  223. Public Const FILE_ATTRIBUTE_NORMAL = &H80
  224. Public Const FILE_ATTRIBUTE_TEMPORARY = &H100
  225. Public Const FILE_ATTRIBUTE_COMPRESSED = &H800
  226.  
  227. ' psfi and cbFileInfo:
  228. ' Address and size, in bytes, of the SHFILEINFO structure that receives the file information.
  229.  
  230. ' Maximun long filename path length
  231. Public Const MAX_PATH = 260
  232.  
  233. Type SHFILEINFO   ' shfi
  234.     hIcon As Long
  235.     iIcon As Long
  236.     dwAttributes As Long
  237.     szDisplayName As String * MAX_PATH
  238.     szTypeName As String * 80
  239. End Type
  240.  
  241. ' uFlags:
  242. ' Flag that specifies the file information to retrieve. This parameter can
  243. ' be a combination of the following values:
  244.  
  245.  
  246. ' Modifies SHGFI_ICON, causing the function to retrieve the file's large icon.
  247. Public Const SHGFI_LARGEICON = &H0&
  248.  
  249. ' Modifies SHGFI_ICON, causing the function to retrieve the file's small icon.
  250. Public Const SHGFI_SMALLICON = &H1&
  251.  
  252. ' Modifies SHGFI_ICON, causing the function to retrieve the file's open icon.
  253. ' A container object displays an open icon to indicate that the container is open.
  254. Public Const SHGFI_OPENICON = &H2&
  255.  
  256. ' Modifies SHGFI_ICON, causing the function to retrieve a shell-sized icon.
  257. ' If this flag is not specified, the function sizes the icon according to the system metric values.
  258. Public Const SHGFI_SHELLICONSIZE = &H4&
  259.  
  260. ' Indicates that pszPath is the address of an ITEMIDLIST structure rather than a path name.
  261. Public Const SHGFI_PIDL = &H8&
  262.  
  263. ' Indicates that the function should use the dwFileAttributes parameter.
  264. Public Const SHGFI_USEFILEATTRIBUTES = &H10&
  265.  
  266. ' Retrieves the handle of the icon that represents the file and the index of the
  267. ' icon within the system image list. The handle is copied to the hIcon member
  268. ' of the structure specified by psfi, and the index is copied to the iIcon member.
  269. ' The return value is the handle of the system image list.
  270. Public Const SHGFI_ICON = &H100&
  271.  
  272. ' Retrieves the display name for the file. The name is copied to the szDisplayName
  273. ' member of the structure specified by psfi. The returned display name uses the
  274. ' long filename, if any, rather than the 8.3 form of the filename.
  275. Public Const SHGFI_DISPLAYNAME = &H200&
  276.  
  277. ' Retrieves the string that describes the file's type. The string is copied to the
  278. ' szTypeName member of the structure specified by psfi.
  279. Public Const SHGFI_TYPENAME = &H400&
  280.  
  281. ' Retrieves the file attribute flags. The flags are copied to the dwAttributes
  282. ' member of the structure specified by psfi.
  283. Public Const SHGFI_ATTRIBUTES = &H800&
  284.  
  285. ' Retrieves the name of the file that contains the icon representing the file.
  286. ' The name is copied to the szDisplayName member of the structure specified by psfi.
  287. Public Const SHGFI_ICONLOCATION = &H1000&
  288.  
  289. ' Returns the type of the executable file if pszPath identifies an executable file.
  290. ' To retrieve the executable file type, uFlags must specify only SHGFI_EXETYPE.
  291. ' The return value specifies the type of the executable file:
  292. ' 0                                                                       Nonexecutable file or an error condition.
  293. ' LOWORD = NE or PEHIWORD = 3.0, 3.5, or 4.0  Windows application
  294. ' LOWORD = MZHIWORD = 0                               MS-DOS .EXE, .COM or .BAT file
  295. ' LOWORD = PEHIWORD = 0                               Win32 console application
  296. Public Const SHGFI_EXETYPE = &H2000&
  297.  
  298. ' Retrieves the index of the icon within the system image list. The index is copied to the iIcon
  299. ' member of the structure specified by psfi. The return value is the handle of the system image list.
  300. Public Const SHGFI_SYSICONINDEX = &H4000&
  301.  
  302. ' Modifies SHGFI_ICON, causing the function to add the link overlay to the file's icon.
  303. Public Const SHGFI_LINKOVERLAY = &H8000&
  304.  
  305. ' Modifies SHGFI_ICON, causing the function to blend the file's icon with the system highlight color.
  306. Public Const SHGFI_SELECTED = &H10000
  307.  
  308.  
  309.  
  310. Public Function BrowseForDirectory(argForm As Form, argFolder As Integer, argDirectory As String, argTitle As String) As Integer
  311.     Dim BI As BROWSEINFO
  312.     Dim nFolder As Long
  313.     Dim IDL As ITEMIDLIST
  314.     Dim pIdl As Long
  315.     Dim sPath As String
  316.     Dim SHFI As SHFILEINFO
  317.   
  318.     BrowseForDirectory = False
  319.     On Error GoTo BrowseForDirectory_Error
  320.     With BI
  321.     
  322. '
  323. ' The dialog's owner window...
  324.     .hOwner = argForm.hWnd
  325. '
  326. ' Set the Browse dialog root folder
  327.     nFolder = argFolder
  328. '
  329. ' Fill the item id list with the pointer of the selected folder item, rtns 0 on success
  330. ' ==================================================
  331. ' If this function fails because the selected folder doesn't exist,
  332. ' .pidlRoot will be uninitialized & will equal 0 (CSIDL_DESKTOP)
  333. ' and the root will be the Desktop.
  334. ' DO NOT specify the CSIDL_ constants for .pidlRoot !!!!
  335. ' The SHBrowseForFolder() call below will generate a fatal exception
  336. ' (GPF) if the folder indicated by the CSIDL_ constant does not exist!!
  337. ' ==================================================
  338.     If (SHGetSpecialFolderLocation(ByVal argForm.hWnd, ByVal nFolder, IDL) = NOERROR) Then
  339.         .pidlRoot = IDL.mkid.cb
  340.     End If
  341. '
  342. ' Initialize the buffer that rtns the display name of the selected folder
  343.     .pszDisplayName = String$(MAX_PATH, 0)
  344. '
  345. ' Set the dialog's banner text
  346.     .lpszTitle = argTitle
  347. '
  348. ' Set the type of folders to display & return
  349. ' -play with these option constants to see what can be returned
  350.     .ulFlags = BIF_RETURNONLYFSDIRS
  351.     End With
  352. '
  353. ' Clear previous return vals before the
  354. ' dialog is shown (it might be cancelled)
  355. '   txtDirectory.Text = ""
  356. '   txtDisplayName = ""
  357. '   pic16Icon.Picture = LoadPicture()   ' clears prev icon
  358. '   pic32Icon.Picture = LoadPicture()
  359. '
  360. ' Show the Browse dialog
  361.     pIdl = SHBrowseForFolder(BI)
  362. '
  363. ' If the dialog was cancelled...
  364.     If (pIdl = 0) Then Exit Function
  365. '
  366. ' Fill sPath w/ the selected path from the id list
  367. ' (will rtn False if the id list can't be converted)
  368.     sPath = String$(MAX_PATH, 0)
  369.     SHGetPathFromIDList ByVal pIdl, ByVal sPath
  370. '
  371. ' Display the path and the name of the selected folder
  372. '   txtDirectory.Text = Left(sPath, InStr(sPath, vbNullChar) - 1)
  373.     argDirectory = Left(sPath, InStr(sPath, vbNullChar) - 1)
  374. '   txtDisplayName = Left$(BI.pszDisplayName, InStr(BI.pszDisplayName, vbNullChar) - 1)
  375. '
  376. ' Get the 16x16 icon info from the id list using the pidl
  377.     SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), SHGFI_PIDL Or SHGFI_ICON Or SHGFI_SMALLICON
  378. ' The 16x16 icon handle rtnd in SHFI.hIcon is stretched to 32x32.
  379. ' DrawIconEx() will shrink (or stretch) the icon per it's cxWidth & cyWidth params
  380. '   DrawIconEx pic16Icon.hdc, 0, 0, SHFI.hIcon, 16, 16, 0, 0, DI_NORMAL
  381. '   pic16Icon.Refresh
  382. '
  383. ' Get the 32x32 icon info from the id list
  384.     SHGetFileInfo ByVal pIdl, 0&, SHFI, Len(SHFI), SHGFI_PIDL Or SHGFI_ICON
  385. ' SHFI.hIcon is OK here so DrawIcon() can be used
  386. '   DrawIcon pic32Icon.hdc, 0, 0, SHFI.hIcon
  387. '   pic32Icon.Refresh
  388. '
  389. ' Frees the memory SHBrowseForFolder()
  390. ' allocated for the pointer to the item id list
  391.     CoTaskMemFree pIdl
  392.     BrowseForDirectory = True
  393.     Exit Function
  394. BrowseForDirectory_Error:
  395.     Exit Function
  396. End Function
  397.